package adventure.placeable;

import java.util.ArrayList;

/**
 *
 * @author Michael Hanns
 *
 */
public class ItemImp extends PlaceableImp implements Item, ItemEdit, Cloneable {

    private String itemType;
    private int modifier;
    private int currencyValue;
    private boolean consumable;
    private int totalUses;
    private int usesLeft;
    private boolean temporary;
    private int roundsRemaining;
    private ArrayList<ItemUnlocks> unlocks;
    private boolean bound;
    

    public ItemImp() {
        super();
        itemType = "invalid";
        modifier = 0;
        currencyValue = 0;
        consumable = false;
        totalUses = 0;
        usesLeft = 0;
        temporary = false;
        roundsRemaining = 0;
        unlocks = new ArrayList<ItemUnlocks>();
        bound = false;
    }

    public ItemImp(int id) {
        super(id);
        itemType = "";
        modifier = 0;
        currencyValue = 0;
        consumable = false;
        totalUses = 0;
        usesLeft = 0;
        temporary = false;
        roundsRemaining = 0;
        unlocks = new ArrayList<ItemUnlocks>();
        bound = false;
    }

    @Override
    public void setItemType(String type) {
        itemType = type;
    }

    @Override
    public void setModifier(int mod) {
        modifier = mod;
    }

    @Override
    public void setCurrencyValue(int value) {
        this.currencyValue = value;
    }
    
    @Override
    public void setConsumable(boolean flag) {
        consumable = flag;
    }

    @Override
    public void setTotalUses(int uses) {
        totalUses = uses;
        usesLeft = totalUses;
    }
    
 
    @Override
    public void setRemainingUses(int uses) {
        usesLeft = uses;
        if(usesLeft > totalUses) {
            usesLeft = totalUses;
        }
    }
    
    @Override
    public void setTemporary(boolean flag) {
        temporary = flag;
    }

    @Override
    public void setTicksRemaining(int uses) {
        roundsRemaining = uses;
    }

    @Override
    public ArrayList<ItemUnlocks> getUnlocks() {
        return unlocks;
    }

    @Override
    public void addUnlocks(ItemUnlocks i) {
        unlocks.add(i);
    }

    @Override
    public void removeUnlocks(ItemUnlocks i) {
        unlocks.remove(i);
    }
    
    @Override
    public void setBound(boolean bound) {
        this.bound = bound;
    }

    @Override
    public String getItemType() {
        return itemType;
    }

    @Override
    public int getModifier() {
        return modifier;
    }
    
    @Override
    public int getCurrencyValue() {
        return currencyValue;
    }

    @Override
    public boolean isConsumable() {
        return consumable;
    }

    @Override
    public int getTotalUses() {
        return totalUses;
    }

    @Override
    public int getUsesLeft() {
        return usesLeft;
    }

    @Override
    public boolean useOnce() {
        if (consumable) {
            if (--usesLeft <= 0) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public boolean isTemporary() {
        return temporary;
    }

    @Override
    public int getTicksRemaining() {
        return roundsRemaining;
    }

    @Override
    public String decay() {
        if (temporary) {
            if (--roundsRemaining <= 0) {
                return getName() + " crumbles into dust.";
            }
        }
        return "";
    }    

    @Override
    public boolean unlocks(int x, int y, int z, char dir) {
        dir = Character.toUpperCase(dir);
        for (int c = 0; c < unlocks.size(); c++) {
            if (unlocks.get(c).unlocks(x, y, z, dir)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void removeAllUnlocks() {
        unlocks = new ArrayList<ItemUnlocks>();
    }
    
    @Override
    public boolean isBound() {
        return bound;
    }

    @Override
    public Item cloneThis() {
        Item newClone = new ItemImp();
        try {
            newClone = (ItemImp) this.clone();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newClone;
    }

    @Override
    public Object clone() {
        try {
            Object clone = super.clone();
            ((ItemImp) clone).unlocks = new ArrayList<ItemUnlocks>();

            for (int c = 0; c < unlocks.size(); c++) {
                ((ItemImp) clone).unlocks.add(unlocks.get(c).cloneThis());
            }

            return clone;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
